package com.ttjj.enums;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 动物朝向
 */
@Slf4j
public enum EnumDirection {
    STOP(0),
    TOP(1),
    DOWN(2),
    LEFT(3),
    RIGHT(4),
//    TOPLEFT(5),
//    TOPRIGHT(6),
//    DOWNLEFT(7),
//    DOWNRIGHT(8)
    ;

    private int value;

    EnumDirection(int value) {
        this.value = value;
    }

    @JSONField(
            serialize = false
    )
    public int get() {
        return this.value;
    }

    public EnumDirection revert() {
        if (this == EnumDirection.TOP) {
            return EnumDirection.DOWN;
        } else if (this == EnumDirection.DOWN) {
            return EnumDirection.TOP;
        } else if (this == EnumDirection.LEFT) {
            return EnumDirection.RIGHT;
        } else if (this == EnumDirection.RIGHT) {
            return EnumDirection.LEFT;
        }
        return this;
    }

    public EnumDirection random() {
        if (this == EnumDirection.STOP) {
            log.info("触发随机命中反向移动");
            Random random = new Random();
            int i = random.nextInt(4);
            return EnumDirection.values()[i];
        }
        return this;
    }

    public EnumDirection random(List<EnumDirection> directions) {
        if (directions.isEmpty()) {
            return EnumDirection.STOP;
        }
        Random random = new Random();
        if (directions.size() == 1) {
            return directions.get(0);
        }
        int i = random.nextInt(directions.size() - 1);
        return directions.get(i);
    }

    /**
     * 即将发生碰撞时，从可选方向中删除掉当前移动的方向
     */
    public List<EnumDirection> getCanDirections(List<EnumDirection> directions) {
        if (directions.isEmpty()) {
            directions.add(EnumDirection.STOP);
            return directions;
        }

        if (this == EnumDirection.TOP) {
            directions.remove(EnumDirection.TOP);
        } else if (this == EnumDirection.DOWN) {
            directions.remove(EnumDirection.DOWN);
        } else if (this == EnumDirection.LEFT) {
            directions.remove(EnumDirection.LEFT);
        } else if (this == EnumDirection.RIGHT) {
            directions.remove(EnumDirection.RIGHT);
        } else if (this == EnumDirection.STOP) {
            directions.remove(EnumDirection.STOP);
        }
        return directions;
    }


}
